package plugins.openform;

import kd.bos.base.AbstractBasePlugIn;
import kd.bos.base.BaseShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;


/**
 * 基础资料插件
 */
public class OpenHealthyData extends AbstractBasePlugIn implements Plugin {
    @Override
    public void preOpenForm(PreOpenFormEventArgs e) {
        super.preOpenForm(e);

        // 获取入口参数
        String identity = e.getFormShowParameter().getCustomParam("Identity");


        if ("user".equals(identity)) {// 获取当前用户名
            String currentUser = RequestContext.get().getUserName();
            // 查询匹配记录（假设基础资料字段标识为"name"）
            QFilter filter = new QFilter("name", QCP.equals, currentUser);
            DynamicObject record = BusinessDataServiceHelper.loadSingle(
                    "dcb4_healthy", // 基础资料实体标识（如：bd_user）
                    "id",
                    new QFilter[]{filter}
            );
            if (record != null) {
                BaseShowParameter baseParam = (BaseShowParameter) e.getSource();
                baseParam.setPkId(record.getPkValue());
                baseParam.setStatus(OperationStatus.EDIT);
            }
        }

    }
}